//
//  ViewController.swift
//  segemNav
//
//  Created by cy on 2017/1/27.
//  Copyright © 2017年 m2micro. All rights reserved.
//

import UIKit

class ViewController: UIViewController,CYSigmentViewDelegate {

    var sigment : CYSigmentView?
    var collectionView : UICollectionView?
    var flowLayOut : UICollectionViewFlowLayout?
    

    override func viewDidLoad() {
        super.viewDidLoad()
        creatSigmentView()
        setCollectionView()
    }
    
    fileprivate func creatSigmentView(){
        let att = ["热门","最新","品类","属性","热门"]
        sigment=CYSigmentView.init(frame: CGRect.init(x: 0, y: 20, width: self.view.width, height: 40), titleArr: att)
        sigment?.delegate=self
        sigment?.backgroundColor=UIColor.orange
        sigment?.titleNomalColor=UIColor.black
        sigment?.titleSelectColor=UIColor.orange
        sigment?.titleFont=UIFont.systemFont(ofSize: 14);

        self.view.addSubview(sigment!)
    
    }
    
    fileprivate func setCollectionView(){
        flowLayOut=UICollectionViewFlowLayout()
        flowLayOut?.minimumLineSpacing=0
        flowLayOut?.minimumInteritemSpacing=0
        flowLayOut?.sectionInset=UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
        flowLayOut?.scrollDirection = . horizontal
        flowLayOut?.itemSize=self.view.size
        collectionView=UICollectionView.init(frame: CGRect.init(x: 0, y: (sigment?.frame.maxY)!, width: self.view.width, height: self.view.height), collectionViewLayout: flowLayOut!)
       collectionView?.backgroundColor=UIColor.yellow
        collectionView?.delegate=self
        collectionView?.dataSource=self
        collectionView?.showsHorizontalScrollIndicator=false
       
        collectionView?.isScrollEnabled=true
         collectionView?.isPagingEnabled=true
        collectionView?.register(UINib.init(nibName: "MYCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "collection_cell")
       
        self.view.addSubview(collectionView!)
    }
    
    func segment(sengment: CYSigmentView, didSelectColumnIndex index: NSInteger) {
        collectionView?.scrollToItem(at: IndexPath.init(row: index-1, section: 0), at: .bottom, animated: false)
        collectionView?.contentOffset=CGPoint.init(x: CGFloat(index-1)*self.view.width, y: 0)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    


}

extension ViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
 
    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let offSetX = (collectionView?.contentOffset.x)!/self.view.width+1
      
        sigment?.collectionScrollPositon(index: Int(offSetX))
        
    }
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (sigment?.titleArray?.count)!
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collection_cell", for: indexPath)
        if indexPath.item==0 {
            cell.backgroundColor=UIColor.red
        }else if indexPath.item==1 {
            cell.backgroundColor=UIColor.gray
        }else if indexPath.item==2 {
            cell.backgroundColor=UIColor.green
        }else if indexPath.item==3 {
            cell.backgroundColor=UIColor.blue
        }else if indexPath.item==4 {
            cell.backgroundColor=UIColor.orange
        }
    
        return cell
        
    }
    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize.init(width: self.view.width, height: self.view.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
     return   UIEdgeInsets.init(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
  
}


